/*
 *@(#)TagImpl.java 1.0 Mar 6, 2008
 *Copyright Fution Bai
 *
 */
package codebush.service.impl;

import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import codebush.domain.Article;
import codebush.domain.TagDataObject;
import codebush.service.Tag;
import codebush.service.support.rowMapper.RowMapperForArticle;
import codebush.service.support.rowMapper.RowMapperForTag;
import codebush.util.ContentUtil;

/**
 * 
 * @author Fution Bai
 * @since 1.0
 */
public class TagImpl extends SimpleJdbcDaoSupport implements Tag {

	/**
	 * the index tag request, just show all tags
	 */
	public Object index() {
		return showAllTags();
	}

	@SuppressWarnings("unchecked")
	private List<String> showAllTags() {
		return getAllDistinctTags();
	}

	/**
	 * show a specific tag's all articles
	 */
	public List<Article> showTagArticles(int tagId) {
		Object[] args = { tagId };
		int[] argTypes = { Types.INTEGER };
		SqlRowSet srs = getJdbcTemplate().queryForRowSet("select a.id, a.title from article as a inner join article_tag as at" + " where a.id=at.article_id and tag_id=?", args, argTypes);
		String[] columns = { "id", "title" };
		List<Article> list = RowMapperForArticle.getInstance().mapRowForSpecificColumns(srs, columns, 0);
		return list;
	}

	/**
	 * get all distinct tags.
	 * 
	 * @param result
	 * @return
	 */
	private List getAllDistinctTags() {
		SqlRowSet srs = getJdbcTemplate().queryForRowSet("select id, name from tag", null, null);
		List result = RowMapperForTag.getInstance().mapRow(srs, 0);
		return result;
	}

	public List<TagDataObject> getTagsByArticleId(long articleId) {
		Object[] args = { articleId };
		int[] argTypes = { Types.BIGINT };
		SqlRowSet srs = getJdbcTemplate().queryForRowSet("select t.id, t.name from tag as t inner join article_tag as at" + " where t.id=at.tag_id and at.article_id=?", args, argTypes);
		List<TagDataObject> list = RowMapperForTag.getInstance().mapRow(srs, 0);
		return list;
	}

	/**
	 * insert a tag into table tag
	 * 
	 * @param tag
	 */
	public void insertTag(String tag) {
		if (tag == null || tag.equals("")) {
			tag = "general";
		}
		String sql = "insert into tag(name) values('" + tag + "')";
		getJdbcTemplate().execute(sql);
	}

	/**
	 * define whether a tag has been exist in the database
	 * 
	 * @param tag
	 * @return true if exists, else false
	 */
	public boolean whetherTagExists(String tag) {
		String sql = "select count(*) from tag where tag.name = '" + tag + "'";
		int i = getJdbcTemplate().queryForInt(sql);
		if (i >= 1) {
			return true;
		}
		return false;
	}

	/**
	 * used to handle the tag before posting an article
	 * 
	 * @param tag
	 */
	public void doTag(String tag) {
		String[] tags = ContentUtil.strWithCommasToStrArray(tag);
		for (int i = 0; i < tags.length; i++) {
			if (!whetherTagExists(tags[i])) {
				insertTag(tags[i]);
			}
		}
	}
}
